<?php
    class EnoriaController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->lng_id = '1';
            
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'enoria','default');
        }

        public function viewAction() {
            if ($this->_getParam('en_id') && $this->_getParam('en_id') != '') {
                $en_id = $this->_getParam('en_id');
            } else {
                $this->_helper->redirector('list', 'enoria', 'default');
            }
            $enoriaModel = new Model_Enoria();
            $enoriaalldata = $enoriaModel->fetchEntryById($en_id);
            $this->view->enoriaalldata = $enoriaalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $enoriasCount = array();


            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
               
                
            } 

            $enoriaModel = new Model_Enoria();
            $enoriasData = $enoriaModel->fetchLISTEnoria($where, $itemsPerPageReviews, $offset, $orderField, $sort,"en_ref_id");
            $enoriasCount = $enoriaModel->fetchLISTEnoria($where);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal(count($enoriasCount));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'enoria', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->enoriasData = $enoriasData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
                        
//$this->view->statusArray = $enoriaModel->getStatusArray();
            //$this->view->searchtype = $searchtype;
        }
        public function deleteAction($ids = Array()) {
            //Load enoria Model 
            $enoriaModel = new Model_Enoria();
            if ($this->_getParam('en_ref_id') && $this->_getParam('en_ref_id') != '') {
                $en_ref_id = $this->_getParam('en_ref_id');
                $enoriaModel->deleteEnoria($en_ref_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'enoria', 'default');
            } else {
                $enoriaModel->deleteEnoria($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'enoria', 'default');
            }
        }
     public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $enoriaData = Array();
            $this->view->mode = 'Add';

            //Load enoria Model
            $enoriaModel = new Model_Enoria();

            //Load enoria Form
            $enoriaForm = new Form_EnoriaForm();

            $areaModel = new Model_Area();
            $areaArray = $areaModel->fetchArea("area_lang_id = ".$this->lng_id."");
            $languageModel = new Model_Language();
            /*$languageArray = $languageModel->fetchlanguageArray();
            $enoriaForm->en_lang_id->addMultiOptions($languageArray);
            
            $areaModel = new Model_Area();
            $areaArray = $areaModel->fetchArea();
            $enoriaForm->area_id->addMultiOptions($areaArray);
            
            
            //Load the status array for the select box in form 
            $statusArray = $enoriaModel->getStatusArray();
            $enoriaForm->en_status->addMultiOptions($statusArray);
            */

            $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['en_identity_code'] = "";
                $languages[$_lan['lang_id']]['en_name'] = "";
                $languages[$_lan['lang_id']]['en_title'] = "";
                $languages[$_lan['lang_id']]['area_id'] = "";
                $languages[$_lan['lang_id']]['en_ref_id'] = "";
                $languages[$_lan['lang_id']]['en_id'] = "";
                
            }
            
            //Check for the enoria Parameter
            $enoriaData['en_ref_id'] = $this->_request->getParam('en_ref_id');
            $en_ref_id = (!empty($enoriaData['en_ref_id'])) ? $enoriaData['en_ref_id'] : null;
            if (!empty($en_ref_id)) {
                $this->view->mode = 'Edit';
                //Fetch data to be loaded in form 
                $data = $enoriaModel->fetchEntryByREFId($en_ref_id);
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['en_lang_id'], $languages)) {
                        $languages[$_desc['en_lang_id']]['en_identity_code'] = $_desc['en_identity_code'];
                        $languages[$_desc['en_lang_id']]['en_name'] = $_desc['en_name'];
                        $languages[$_desc['en_lang_id']]['en_title'] = $_desc['en_title'];
                        $languages[$_desc['en_lang_id']]['area_id'] = $_desc['area_id'];
                        $languages[$_desc['en_lang_id']]['en_id'] = $_desc['en_id'];
                        $languages[$_desc['en_lang_id']]['en_ref_id'] = $en_ref_id;
                        
                    }
                }
            }
            //Populate form 
            //$this->view->form = $enoriaForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                
               /*  if($postedData['en_identity_code']== ''){
                    $errorMessageArray[] = 'Please Enter identity code';
                }
                if($postedData['en_name']== ''){
                    $errorMessageArray[] = 'Please enter name';
                }*/
                if($postedData['area_id']== '' ){
                    $errorMessageArray[] = 'Please select area';
                }
               /* $languages[1]['en_identity_code'] = $postedData['en_identity_code'];
                $languages[1]['en_name'] = $postedData['en_name'];
                */$languages[1]['area_id'] = $postedData['area_id'];
                
                
                
                if($postedData['1_en_title']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                      /*  $languages[$key]['en_identity_code'] = $postedData['en_identity_code'];
                        $languages[$key]['en_name'] = $postedData['en_name'];
                        */$languages[$key]['en_title'] = $postedData[$_lang['lang_id'].'_en_title'];
                        $languages[$key]['area_id'] = $postedData['area_id'];
                        
                    }
                }    
                
                    if (count($errorMessageArray) > 0) {
                    } else {
                        if ($this->view->mode == 'Add') {
                            $enoriaData['en_createddate'] = date('Y-m-d H:i:s');
                        }
                        //enoria save
                        /*$enoriaData['en_id'] = $en_id;
                        $enoriaData['en_identity_code'] = $postedData['en_identity_code'];
                        $enoriaData['en_name'] = $postedData['en_name'];
                        $enoriaData['en_title'] = $postedData['en_title'];
                        $enoriaData['en_lang_id'] = $postedData['en_lang_id'];
                        $enoriaData['area_id'] = $postedData['area_id'];
                        $enoriaData['en_updateddate'] = date('Y-m-d H:i:s');
                        $enoriaData['en_status'] = $postedData['en_status'];
                        $enoriaModel->save($enoriaData, 'en_id');
                        */
                         if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = strtolower($postedData[$_lang['lang_id'].'_en_title']);
                                $enoriaData['en_identity_code'] = strtolower($english_code);
                                $enoriaData['en_name'] = strtolower($english_code);
                                $enoriaData['en_title'] = $postedData[$_lang['lang_id'].'_en_title'];
                                $enoriaData['area_id'] = $postedData['area_id'];
                                $enoriaData['en_createddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_updateddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_lang_id'] = $_lang['lang_id'];
                                $enoriaData['en_status'] = 1;
                                $enoriaData['en_ref_id'] = $_lang['en_ref_id'];
                                $last_id = $enoriaModel->save($enoriaData, 'en_id');
                                $enoriaData1['en_id'] = $last_id;
                                $enoriaData1['en_ref_id'] = $last_id;
                                $enoriaModel->save($enoriaData1, 'en_id');
                            }else{
                                $enoriaData['en_identity_code'] = strtolower($english_code);
                                $enoriaData['en_name'] = strtolower($english_code);
                                $enoriaData['en_title'] = $postedData[$_lang['lang_id'].'_en_title'];
                                $enoriaData['area_id'] = $postedData['area_id'];
                                $enoriaData['en_createddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_updateddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_lang_id'] = $_lang['lang_id'];
                                $enoriaData['en_status'] = 1;
                                $enoriaData['en_ref_id'] = $last_id;
                                if($enoriaData['en_name']!= '' ){
                                    $enoriaModel->save($enoriaData, 'en_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code =  $postedData[$_lang['lang_id'].'_en_title'];
                                $enoriaData['en_id'] = $_lang['en_id'];
                                $enoriaData['en_identity_code'] = strtolower($english_code);
                                $enoriaData['en_name'] = strtolower($english_code);
                                $enoriaData['en_title'] = $postedData[$_lang['lang_id'].'_en_title'];
                                $enoriaData['area_id'] = $postedData['area_id'];
                                $enoriaData['en_lang_id'] = $_lang['lang_id'];
                                $enoriaData['en_status'] = 1;
                                $enoriaData['en_updateddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_ref_id'] = $en_ref_id;
                                $enoriaModel->save($enoriaData, 'en_id');
                                
                            }else{
                                $enoriaData['en_id'] = $_lang['en_id'];
                                $enoriaData['en_identity_code'] = strtolower($english_code);
                                $enoriaData['en_name'] = strtolower($english_code);
                                $enoriaData['en_title'] = $postedData[$_lang['lang_id'].'_en_title'];
                                $enoriaData['area_id'] = $postedData['area_id'];
                                $enoriaData['en_lang_id'] = $_lang['lang_id'];
                                $enoriaData['en_status'] = 1;
                                $enoriaData['en_updateddate'] = date('Y-m-d H:i:s');
                                $enoriaData['en_ref_id'] = $en_ref_id;
                                $enoriaModel->save($enoriaData, 'en_id');
                                
                            }
                        }
                     
                   }
                        
                        if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'Enoria add successfully';
                            } else {
                                $successmessage = 'Enoria updated successfully';
                            }
                            $this->addSuccessMessage($successmessage);
                            $this->_helper->redirector('list', 'enoria', 'default');
                        }
                    }
                
            }
            $this->view->areaArray = $areaArray;
            $this->view->languages = $languages;
            $this->view->enoriaid = $en_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            //$this->view->form = $enoriaForm;


    }
    }?>